<?php

namespace App\Providers\Filament;

use Closure;
use Filament\Facades\Filament;
use Filament\Http\Middleware\Authenticate;
use Filament\Http\Middleware\DisableBladeIconComponents;
use Filament\Http\Middleware\DispatchServingFilamentEvent;
use Filament\Pages;
use Filament\Panel;
use Filament\PanelProvider;
use Filament\Support\Assets\Css;
use Filament\Support\Colors\Color;
use Filament\Widgets;
use Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse;
use Illuminate\Cookie\Middleware\EncryptCookies;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken;
use Illuminate\Routing\Middleware\SubstituteBindings;
use Illuminate\Session\Middleware\AuthenticateSession;
use Illuminate\Session\Middleware\StartSession;
use Illuminate\View\Middleware\ShareErrorsFromSession;
use App\Filament\User\Pages\UserDashboard;
use Filament\Navigation\MenuItem;
use App\Filament\User\Pages\Profile;
use App\Filament\User\Pages\Login;
use App\Filament\User\Pages\Register;
use Filament\Support\Facades\FilamentView;
use Filament\View\PanelsRenderHook;
use Illuminate\Contracts\View\View;
use Filament\Support\Facades\FilamentAsset;
use Filament\Support\Assets\Js;
use Filament\Support\Enums\MaxWidth;
use Filament\FontProviders\LocalFontProvider;

class UserPanelProvider extends PanelProvider
{
    public function panel(Panel $panel): Panel
    {
        return $panel
            ->id('user')
            ->font(
                'Inter',
                url: asset('css/fonts.css'),
                provider: LocalFontProvider::class,
            )
            ->path('user')
            ->login(Login::class)
            ->registration(Register::class)
            ->emailVerification()
            ->colors([
                'primary' => Color::Amber,
            ])
            ->darkMode(true, true)
            ->breadcrumbs(false)
            ->discoverResources(in: app_path('Filament/User/Resources'), for: 'App\\Filament\\User\\Resources')
            ->discoverPages(in: app_path('Filament/User/Pages'), for: 'App\\Filament\\User\\Pages')
            ->pages([
                UserDashboard::class,
            ])
            ->discoverWidgets(in: app_path('Filament/User/Widgets'), for: 'App\\Filament\\User\\Widgets')
            ->widgets([
               // Widgets\AccountWidget::class,
               // Widgets\FilamentInfoWidget::class,
            ])
            ->plugins([
                \ChrisReedIO\Socialment\SocialmentPlugin::make(),
            ])
            ->maxContentWidth(MaxWidth::Full)
            ->middleware([
                EncryptCookies::class,
                AddQueuedCookiesToResponse::class,
                StartSession::class,
                AuthenticateSession::class,
                ShareErrorsFromSession::class,
                VerifyCsrfToken::class,
                SubstituteBindings::class,
                DisableBladeIconComponents::class,
                DispatchServingFilamentEvent::class,
            ])
            ->authMiddleware([
                Authenticate::class,
            ])->viteTheme('resources/css/filament/user/theme.css')
              ->databaseNotifications()
              ->topNavigation()
            ->userMenuItems([
                MenuItem::make()
                    ->label('修改资料')
                    ->url(fn (): string => "/user/profile")
                    ->icon('heroicon-o-user-circle'),
                MenuItem::make()
                    ->label('余额记录')
                    ->url(fn (): string => "/user/balances")
                    ->icon('heroicon-o-currency-dollar'),
                MenuItem::make()
                    ->label('交易记录')
                    ->url(fn (): string => "/user/orders")
                    ->icon('heroicon-o-credit-card'),
                MenuItem::make()
                    ->label('我的工程')
                    ->url(fn (): string => "/user/projects")
                    ->icon('heroicon-o-folder'),
                MenuItem::make()
                    ->label('我的工单')
                    ->url(fn (): string => "/user/tickets")
                    ->icon('heroicon-o-folder'),
                MenuItem::make()
                    ->label('我的收藏')
                    ->url(fn (): string => "/user/favorites")
                    ->icon('heroicon-o-star'),
            ])->renderHook(
                'panels::global.search.end',
                fn () => view('project-btn'),
            );
    }

    public function boot():void
    {
        FilamentAsset::register([
            Css::make('custom-stylesheet', base_path('/resources/css/custom.css'))->loadedOnRequest(),
        ]);
    }
}
